import query from './atom/db/query';
import { IBaseTable } from './atom/db/interfaces';

declare const action_id: string;
declare const session_id: string;
declare const spaceid: string;

interface Message {
	filter: {};
}

interface deskScreen extends IBaseTable {
	no: string; user_code: string; user_name: string; screen_no: string;
}

interface quickMenu extends IBaseTable {
	no: string; screen_no: string; menu_order: number;
}

async function atom(msg: Message) {
	const systemid = '6051';
	const { exec, prepare } = query(action_id, session_id, spaceid, systemid);

	// 桌面分屏
	prepare('desk_screen', ['no', 'user_code', 'user_name', 'screen_no'], msg.filter, 0, 1, ['screen_no']);
	const [data] = await exec();

	const nos = data.map((d: deskScreen) => {
		return d.no;
	});

	const q = query(action_id, session_id, spaceid, systemid);
	q.prepare('quick_menu', ['no', 'user_code', 'user_name', ' screen_no', 'menu_no', 'menu_name', 'menu_icon', 'menu_order'], { screen_no: { $in: nos } });
	const [m_data] = await q.exec();

	const res = data.map((d: deskScreen) => {
		const no = d.no;
		d['menus'] = m_data.filter((m: quickMenu) => {
			return m.screen_no == no;
		});
		return d;
	});

	return res;
}